package com.cuitrip.business.tripservice.ui;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.lab.component.common.BaseDialogFragment;
import com.lab.logtrack.AppLog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 上传图片dialog
 * Created by ak
 */
public class UploadDialog extends BaseDialogFragment {

    @Bind(android.R.id.progress)
    ProgressBar mProgress;
    @Bind(R.id.upload_progress)
    TextView mUploadProgress;
    @Bind(R.id.progress_text)
    TextView mProgressPercentText;
    @Bind(R.id.cancel_action)
    TextView mCancelAction;
    @Bind(R.id.choice_dialog_root_layout)
    FrameLayout mChoiceDialogRootLayout;
    private View mRootView;
    private CharSequence progressText;
    private CharSequence progressPercent;


    private int mDialogWidthPixels;
    private int mDialogHeightPixels;

    private DialogCancelCallback mCancelCallback;

    public static UploadDialog getInstance() {
        UploadDialog choiceDialog = new UploadDialog();
        return choiceDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHideWhenStop(false);
        setCancelable(isCancelable());
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.choiceDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.ct_layout_upload_dialog, null, false);
        ButterKnife.bind(this, mRootView);
        mUploadProgress.setText(progressText);
        mProgressPercentText.setText(progressPercent);
        return mRootView;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        return dialog;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        try {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int defaultDialagWidth = getDialog().getWindow().getAttributes().width;
            int defaultDialogHeight = getDialog().getWindow().getAttributes().height;
            int specifyDialogWidth = defaultDialagWidth;
            int specifyDialogHeight = defaultDialogHeight;
            if (mDialogWidthPixels < 0) {
                specifyDialogWidth = dm.widthPixels;
            } else if (mDialogWidthPixels > 0) {
                specifyDialogWidth = mDialogWidthPixels;
            } else {
                specifyDialogWidth = defaultDialagWidth;
            }
            if (mDialogHeightPixels < 0) {
                specifyDialogHeight = dm.heightPixels;
            } else if (mDialogHeightPixels > 0) {
                specifyDialogHeight = mDialogHeightPixels;
            } else {
                specifyDialogHeight = defaultDialogHeight;
            }
            getDialog().getWindow().setLayout(specifyDialogWidth, specifyDialogHeight);
        } catch (Exception e) {
            AppLog.w(e.getMessage());
        }
    }

    public void setProgress(int percent) {
        setProgressPercent(percent);
        if (mProgressPercentText != null) {
            mProgressPercentText.setText(progressPercent);
        }
    }

    public void setProgressInTotal(int index, int total) {
        setProgressText(index, total);
        if (mUploadProgress != null) {
            mUploadProgress.setText(progressText);
        }
    }

    private void setProgressText(int index, int total) {
        StringBuilder stringBuilder = new StringBuilder(String.valueOf(index));
        stringBuilder.append("/").append(total);
        this.progressText = stringBuilder.toString();
    }

    private void setProgressPercent(int percent) {
        StringBuilder stringBuilder = new StringBuilder(String.valueOf(percent));
        stringBuilder.append("%");
        this.progressPercent = stringBuilder.toString();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.cancel_action)
    public void onClick() {
        if (mCancelCallback != null) {
            mCancelCallback.onCancelClick();
        }
    }

    public void setCancelCallback(DialogCancelCallback mCancelCallback) {
        this.mCancelCallback = mCancelCallback;
    }

    public interface DialogCancelCallback {

        boolean onCancelClick();

    }


}
